package com.orangelife.mobile.tehui;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.curry.android.util.DecimalUtil;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;
import java.util.Map;

/**
 * Created by YangWei
 * on 2016/4/1.
 */
public class GoodsGridAdapter extends BaseAdapter {
    private List<Map<String, Object>> list;
    private Context mContext;
    private int status;
    private TehuiInf inf;

    public GoodsGridAdapter(List<Map<String, Object>> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
    }

    public void setList(List<Map<String, Object>> list) {
        this.list = list;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grid_goods, null, false);
            holder.ivGoods = (ImageView) convertView.findViewById(R.id.ivGoods);
            holder.tvYuanjia = (TextView) convertView.findViewById(R.id.tvPrice2);
            holder.tvTejia = (TextView) convertView.findViewById(R.id.tvPrice1);
            holder.tvTj = (TextView) convertView.findViewById(R.id.tvTj);
            holder.tvBuy = (TextView) convertView.findViewById(R.id.tvBuy);
            holder.tvSoldNum = (TextView) convertView.findViewById(R.id.tvSoldNum);
            holder.tvGoodsName1 = (TextView) convertView.findViewById(R.id.tvGoodsName1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Map<String, Object> map = list.get(position);
        String skuName = String.valueOf(map.get("skuName"));//商品名称
        String marketPrice = String.valueOf(map.get("marketPrice"));//市场价格
        String activityPrice = String.valueOf(map.get("activityPrice"));//活动价格
        String soldNumber = String.valueOf(map.get("soldNumberNew"));//已售数量
        String goodsImgs = String.valueOf(map.get("goodsImgs"));
        int stockNumberNew = Integer.parseInt(String.valueOf(map.get("stockNumberNew")));//销售后库存

        ImageLoader.getInstance().displayImage(goodsImgs, holder.ivGoods);
        holder.tvYuanjia.setText("原价" + DecimalUtil.decimalFormat(marketPrice));
        holder.tvGoodsName1.setText(skuName);
        holder.tvTejia.setText(DecimalUtil.decimalFormat(activityPrice));
        if (StringUtil.isBlank(soldNumber)) {
            soldNumber = "0";
        }
        holder.tvSoldNum.setText("已售" + soldNumber + "件");
        if (stockNumberNew <= 0) {
            holder.tvSoldNum.setText("已售罄");
        }
        holder.tvYuanjia.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        if (status != 1) {//活动状态 0:未开始 1:进行中 2;结束
            holder.tvBuy.setText("开抢提醒");
            holder.tvSoldNum.setVisibility(View.INVISIBLE);
            holder.tvBuy.setBackgroundColor(mContext.getResources().getColor(R.color.lightOrange));
            holder.tvTejia.setTextColor(mContext.getResources().getColor(R.color.lightOrange));
            holder.tvTj.setTextColor(mContext.getResources().getColor(R.color.lightOrange));
        } else {
            holder.tvBuy.setText("马上抢购");
            holder.tvSoldNum.setVisibility(View.VISIBLE);
            holder.tvBuy.setBackgroundColor(mContext.getResources().getColor(R.color.text_orange));
            holder.tvTejia.setTextColor(mContext.getResources().getColor(R.color.text_orange));
            holder.tvTj.setTextColor(mContext.getResources().getColor(R.color.text_orange));
        }
        holder.tvBuy.setOnClickListener(new MyListener(map));
        return convertView;
    }

    private class MyListener implements View.OnClickListener {

        private Map<String, Object> map;

        public MyListener(Map<String, Object> map) {
            this.map = map;
        }

        @Override
        public void onClick(View v) {

            if (status == 1) {
                inf.buy(map);
            } else {
                inf.remind(map);
            }

        }
    }

    /**
     * 按钮事件回调
     */
    interface TehuiInf {
        void buy(Map<String, Object> map);

        void remind(Map<String, Object> map);
    }

    public void setInf(TehuiInf inf) {
        this.inf = inf;
    }

    class ViewHolder {
        TextView tvBuy;
        TextView tvYuanjia;
        TextView tvTj;
        TextView tvTejia;
        TextView tvSoldNum;
        TextView tvGoodsName1;
        ImageView ivGoods;
    }

}
